#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <algorithm> //reverse
#include <cmath>
using namespace std;

struct STU
{
	string name;
	int year;
	int month;
	int day;
	int id;
}arr[100];

bool compare(struct STU s1, struct STU s2)
{
	if (s1.year != s2.year)
		return s1.year < s2.year;
	else if (s1.month != s2.month)
		return s1.month < s2.month;
	else if (s1.day != s2.day)
		return s1.day < s2.day;
	else
		return s1.id > s2.id;
}
int main()
{
	int N = 0;
	cin >> N;

	for (int i = 0; i < N; i++)
	{
		cin >> arr[i].name >> arr[i].year >> arr[i].month >> arr[i].day;
		arr[i].id = i;
	}
	sort(arr, arr + N, compare);
	for (int i = 0; i < N; i++)
	{
		cout << arr[i].name << endl;
	}
	return 0;
}